/*
 * File         : cym_list.c
 * Version      : 0.1.0
 * Author       : Vincent Cheung
 * Date         : Sept. 23, 2013
 * Description  : All the operation of list for this project.
 *
 * Copyright (C) 2013, CYM Co., Ltd.
 *
 * Change Logs  :
 * Date                 Author          Notes
 * Sept. 23, 2013       Vincent         first version
 *
 */

#include "cym_def.h"
#include "cym_list.h"

#ifndef	CYM_USING_INLINE
/**
 * @addtogroup Kernel
 */

/*@{*/

/**
 * This function is used to initialize a list.
 *
 * @param list a list to be initialized
 *
 * @retval NULL
 */
void cym_list_init(list_t *list)
{
	list->next = list->prev = list;
}

/**
 * This function is used to insert a new node after a list.
 *
 * @param list list to insert into it
 * @param node a new node to be inserted
 *
 * @retval NULL
 */
void cym_list_insert_after(list_t *list, list_t *node)
{
	list->next->prev = node;
	node->next = list->next;

	list->next = node;
	node->prev = list;
}

/**
 * This function is used to insert a new node before a list.
 *
 * @param list a list to insert into it
 * @param node a new node to be inserted
 *
 * @retval NULL
 */
void cym_list_insert_before(list_t *list, list_t *node)
{
	list->prev->next = node;
	node->prev = list->prev;

	list->prev = node;
	node->next = list;
}

/**
 * This function is used to remove a node from a list.
 *
 * @param node the node to remove from the list
 *
 * @retval NULL
 */
void cym_list_remove(list_t *node)
{
	node->next->prev = node->prev;
	node->prev->next = node->next;

	node->next = node->prev = node;
}

/**
 * This function is used to check whether the list is empty.
 *
 * @param list the list to be checked
 *
 * @return return CYM_TRUE if the list is empty
 */
bool_t cym_list_is_empty(const list_t *list)
{
	if (list->next == list && list->next == list->prev)
	{
		return CYM_TRUE;
	}

	return CYM_FALSE;
}

/*@}*/
#endif
